load(
    "@io_bazel_rules_docker//container:container.bzl",
    "container_image",
)
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

genrule(
    name = "alpine-iso",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": ["@alpine_image_ppc64le//file"],
        "//conditions:default": ["@alpine_image//file"],
    }),
    outs = ["usr/share/nginx/html/images/alpine.iso"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "mkdir -p usr/share/nginx/html/images && cat $(location @alpine_image_ppc64le//file) > $@",
        "//conditions:default": "mkdir -p usr/share/nginx/html/images && cat $(location @alpine_image//file) > $@",
    }),
)

pkg_tar(
    name = "alpine-tar",
    srcs = [
        ":alpine-iso",
    ],
    mode = "644",
    package_dir = "usr/share/nginx/html/images",
)

genrule(
    name = "cirros-img",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": ["@cirros_image_ppc64le//file"],
        "//conditions:default": ["@cirros_image//file"],
    }),
    outs = ["usr/share/nginx/html/images/cirros.img"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "mkdir disk && cat $(location @cirros_image_ppc64le//file) > $@",
        "//conditions:default": "mkdir disk && cat $(location @cirros_image//file) > $@",
    }),
)

pkg_tar(
    name = "cirros-img-tar",
    srcs = [":cirros-img"],
    mode = "644",
    package_dir = "usr/share/nginx/html/images",
)

genrule(
    name = "fedora-img",
    srcs = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": ["@fedora_image_ppc64le//file"],
        "//conditions:default": ["@fedora_image//file"],
    }),
    outs = ["usr/share/nginx/html/images/fedora.img"],
    cmd = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "mkdir disk && cat $(location @fedora_image_ppc64le//file) > $@",
        "//conditions:default": "mkdir disk && cat $(location @fedora_image//file) > $@",
    }),
)

pkg_tar(
    name = "fedora-img-tar",
    srcs = [":fedora-img"],
    mode = "644",
    package_dir = "usr/share/nginx/html/images",
)

pkg_tar(
    name = "nginx-config-tar",
    srcs = [
        "nginx.conf",
    ],
    mode = "664",
    package_dir = "/etc/nginx",
)

container_image(
    name = "cdi-http-import-server-image",
    architecture = select({
        "@io_bazel_rules_go//go/platform:linux_ppc64le": "ppc64le",
        "//conditions:default": "amd64",
    }),
    base = "//images:kubevirt-testing-base",
    directory = "/",
    entrypoint = ["/entrypoint.sh"],
    files = [
        "entrypoint.sh",
        "expose-as-iscsi.sh",
    ],
    ports = ["80"],
    tars = [
        ":alpine-tar",
        ":cirros-img-tar",
        ":fedora-img-tar",
        ":nginx-config-tar",
    ],
    visibility = ["//visibility:public"],
)
